package Clients;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.text.NumberFormat;
import java.util.Locale;

import Catalogue.*;
import Middle.*;

/**
 * Implements the GUI for the Customer client.
 * @author  Michael Alexander Smith
 * @version 2.0
 */


class CustomerGUI
{
  class Name                              // Names of buttons
  {
    public static final String CHECK  = "Check";
    public static final String CLEAR  = "Clear";
  }

  private static final int H = 300;       // Height of window pixels
  private static final int W = 400;       // Width  of window pixels

  private JLabel      action  = new JLabel();
  private JTextField  input   = new JTextField();
  private JTextArea   output  = new JTextArea();
  private JScrollPane scrollPane      = new JScrollPane();
  private JButton     checkBtn = new JButton( Name.CHECK );
  private JButton     clearBtn = new JButton( Name.CLEAR );

  private Picture     picture = new Picture(80,80);
  private Transaction transaction      = new Transaction();
  private Basket      basket  = new Basket();
  private StockReader stock   = null;


  private NumberFormat theMoney  =
          NumberFormat.getCurrencyInstance( Locale.UK );

  public CustomerGUI( RootPaneContainer rpc, MiddleFactory mf )
  {
    try                                             // 
    {      
      stock  = mf.makeStockReader();             // DataBase Access
    } catch ( Exception e )
    {
      System.out.println("Exception: " + e.getMessage() );
    }
    Container container         = rpc.getContentPane();    // Content Pane
    Container rootWindow = (Container) rpc;         // Root Window
    container.setLayout(null);                             // No layout manager
    rootWindow.setSize( W, H );                     // Size of Window
    container.setBackground(Color.BLACK);

    Font f = new Font("Monospaced",Font.PLAIN,12);  // Font f is

    checkBtn.setBounds( 16, 25+60*0, 80, 40 );    // Check button
    checkBtn.addActionListener( transaction );          //  Listener
    checkBtn.setIcon(new ImageIcon("src/images/ButtonTex/buttonTex.png")); 	//Changes the background image
    checkBtn.setVerticalTextPosition(JButton.CENTER); 						// Sets the text to the middle
    checkBtn.setHorizontalTextPosition(JButton.CENTER); 						//to avoid it bumping the image around
    checkBtn.setBackground(Color.BLACK); 			//Sets the background to black
    checkBtn.setBorder(null); 					//Removes the border around the button
    checkBtn.setForeground(Color.WHITE); 			//Sets the font color to white
    checkBtn.setContentAreaFilled(false); 		//Does not allow the background to change when clicked
    container.add( checkBtn );                           //  Add to canvas

    clearBtn.setBounds( 16, 25+60*1, 80, 40 );    // Clear button
    clearBtn.addActionListener( transaction );          //  Listener
    clearBtn.setIcon(new ImageIcon("src/images/ButtonTex/buttonTex.png")); 	//Changes the background image
    clearBtn.setVerticalTextPosition(JButton.CENTER); 						// Sets the text to the middle
    clearBtn.setHorizontalTextPosition(JButton.CENTER); 						//to avoid it bumping the image around
    clearBtn.setBackground(Color.BLACK); 			//Sets the background to black
    clearBtn.setBorder(null); 					//Removes the border around the button
    clearBtn.setForeground(Color.WHITE); 			//Sets the font color to white
    clearBtn.setContentAreaFilled(false); 		//Does not allow the background to change when clicked
    container.add( clearBtn );                           //  Add to canvas

    action.setBounds( 110, 235 , 270, 20 );       // Message area
	action.setText("Please input the product number.");
	action.setForeground(Color.WHITE);
    container.add( action );                            //  Add to canvas

    input.setBounds( 110, 25, 270, 40 );         // Product no area
    input.setText("");                           // Blank
	input.setBackground(Color.BLACK);
	input.setForeground(Color.WHITE);
	input.setBorder(BorderFactory.createLineBorder(Color.green));
    container.add( input );                             //  Add to canvas
    
    scrollPane.setBounds( 110, 75, 270, 160 );          // Scrolling pane
    output.setText( "" );                        //  Blank
    output.setFont( f );                         //  Uses font
    output.setEditable(false);
	scrollPane.setBorder(null);
	output.setBackground(Color.BLACK);
	output.setForeground(Color.WHITE);
	output.setBorder(BorderFactory.createLineBorder(Color.green));
    container.add( scrollPane );                                //  Add to canvas
    scrollPane.getViewport().add( output );           //  In TextArea

    picture.setBounds( 16, 25+60*2, 260, 260 );   // Picture area
    picture.setDefLoc(new Point(16, 25+60*2));
    container.add( picture );                           //  Add to canvas
    picture.clear();
    
    rootWindow.setVisible( true );                  // Make visible
  }

  class Transaction implements ActionListener       // Listener
  {
    public void actionPerformed( ActionEvent ae )   // Interaction
    {
      if ( stock == null )
      {
        action.setText("No connection");
        return;                                     // No connection
      }
      String actionIs = ae.getActionCommand();      // Which button
      String pn  = input.getText().trim();       // product no.
      int    amount  = 1;                           //  & quantity

      try
      {
        if ( actionIs.equals( Name.CHECK ) )        // Button CHECK
        {
          picture.clear();                       // Clear picture
          if ( stock.exists( pn ) )              // Stock Exists?
          {                                         // T
            Product pr = stock.getDetails( pn ); //  Product
            if ( pr.getQuantity() >= amount )       //  In stock?
            {                                       //  T
              action.setText(                    //   Display 
                pr.getDescription() + " : " +       //    description
                theMoney.format(pr.getPrice()) +    //    price
                " (" + pr.getQuantity() + ")"       //    quantity
              );                                    //   of product
              pr.setQuantity( amount );             //   Require 1
              basket.add( pr );                  //   Add to basket
              output.setText(                    //   Display
                basket.getDetails() );           //   shopping list
              picture.set(                       //   Picture of
                stock.getImage( pn ) );          //    product
            } else {                                //  F
              action.setText(                    //   Inform
                pr.getDescription() +               //    product not
                " not in stock"                     //    in stock
              );
            }
          } else {                                  // F
            action.setText(                      //  Inform Unknown
              "Unknown product number " + pn        //  product number
            );
          }
        }
  
        if ( actionIs.equals( Name.CLEAR ) )        // Button CLEAR
        {
          basket.clear();
			action.setText("Please input the product number.");
			output.setText("");
			input.setText("");
			picture.clear();
        }
        
        input.requestFocus();                    // Focus        
      }
      catch ( StockException e )                    // Error
      {                                             //  Of course
        output.append( "Error:" + "\n" +         //  Should not
                            e.getMessage() + "\n" );//  happen
      }
    }
    
  }
  
}


